package com.elangzhi.ssm.modules.record.app;

import com.elangzhi.ssm.controller.json.Tip;
import com.elangzhi.ssm.model.Message;
import com.elangzhi.ssm.model.Record;
import com.elangzhi.ssm.model.Talk;
import com.elangzhi.ssm.model.User;
import com.elangzhi.ssm.modules.message.services.MessageService;
import com.elangzhi.ssm.modules.record.services.RecordService;
import com.elangzhi.ssm.modules.talk.services.TalkService;
import com.elangzhi.ssm.modules.user.services.UserService;
import com.elangzhi.ssm.tools.UUIDFactory;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* 说说评论 客户端 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/app/record")
public class AppRecordController {

    /**
     * 获取评论列表
     * @param talkId 说说id
     * @param page 第几页
     * @param size 每页大小
     * @return 列表
     */
    @RequestMapping("/listByTalk")
    @ResponseBody
    public PageInfo<Record> listByTalk(Long talkId,
                                       @RequestParam Integer page,
                                       @RequestParam Integer size){

        PageInfo<Record> records = recordService.listByTalk(talkId,page,size);
        for(Record t : records.getList()){
            t.setUser(userService.selectById(t.getUserId()));
            t.setChild(recordService.listByTypeAdnInfoId(2,t.getId()));
        }
        return records;
    }

    /**
     * 提交评论
     * @param record 评论内容
     *               {
     *               userId
     *               content
     *               type:0,赞 1,说说评论 2，评论的评论
     *               infoId 对应type的id 如果type为2 ，所有infoId都传最顶层评论的id
     *               }
     * @return 成功状态
     */
    @RequestMapping("/submit")
    @ResponseBody
    public Tip submit(Record record){

        /**
         * 判断我是否赞过，赞过的话就删掉这条数据
         */
        if(record.getType() == 0 && isHaveZan(record.getInfoId(),record.getUserId())){
            return new Tip(2,"已删除赞的记录，再次点击将重新添加。");
        }

        record.setId(UUIDFactory.getLongId());
        record.setSetTime(new Date());
        try {
            recordService.insert(record);
            insertMessage(record);
            return new Tip(record);
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1);
        }
    }

    /**
     * 判断某人是否已经赞过
     * @param talkId 说说id
     * @param userId 用户id
     * @return 是否赞过
     */
    public boolean isHaveZan(Long talkId,Long userId){
        List<Record> zanList = recordService.listByTypeAdnInfoId(0,talkId);
        for(Record record : zanList){
            if(userId.equals(record.getUserId())){

                try {
                    recordService.deleteById(record.getId());
                    messageService.deleteByRecord(record.getId());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return true;
            }
        }
        return false;
    }

    /**
     * 添加消息
     * @param record 消息
     */
    private void insertMessage(Record record) {

        Long receiveId;
        if(record.getType() == 2){
            receiveId = record.getReceiveId();
        }else{
            Talk talk = talkService.selectById(record.getInfoId());
            receiveId = talk.getUserId();
        }

        try {
            Message message = new Message();
            message.setId(UUIDFactory.getLongId());
            message.setRecordId(record.getId());
            message.setSetTime(new Date());
            message.setSend(record.getUserId());
            message.setReceive(receiveId);
            message.setType(record.getType());
            message.setStatus(0);
            message.setInfoId(record.getInfoId());
            message.setContent(record.getContent());
            messageService.insert(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除评论
     * @param id 评论id
     * @return 成功状态
     */
    @RequestMapping("/deleteRecord")
    @ResponseBody
    public Tip deleteRecord(Long id){
        try {
            messageService.deleteByRecord(id);
            recordService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            System.out.println("删除评价失败");
            return new Tip(1);
        }
    }



    //---------------------------- property -------------------------------

    @Resource
    private RecordService recordService;

    @Resource
    private TalkService talkService;

    @Resource
    private MessageService messageService;

    @Resource
    private UserService userService;
}
